package cn.team11.smarteye.base;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.team11.sds.dayi.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.team11.smarteye.utils.AppLogger;

/**
 * Created by Murphy on 2017/11/3.
 */

public abstract class BaseDialog extends DialogFragment implements View.OnClickListener{

    @BindView(R.id.dialog_generic_title)
    TextView mGenerateTitleTv;
    @BindView(R.id.dialog_generic_content)
    FrameLayout mDialogContent;
    @BindView(R.id.dialog_left_positive_btn)
    Button mDialogLeftBt;
    @BindView(R.id.dialog_right_negative_btn)
    Button mDialogRightBt;
    @BindView(R.id.custom_dialog_linearlayout)
    LinearLayout mDialogLayout;

    private LayoutInflater inflater;
    private OnBtClickListener onBtClickListener;

    //public static final int LEFT_BUTTON_CLICK = 1;
    //public static final int RIGHT_BUTTON_CLICK = 2;

    /**
     * 构造函数
     * */
    public BaseDialog() {
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.base_custom_dialog_layout, null);
        builder.setView(view);
        ButterKnife.bind(this, view);
        mDialogLeftBt.setOnClickListener(this);
        mDialogRightBt.setOnClickListener(this);
        return builder.create();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.dialog_left_positive_btn){
            onBtClickListener.onConfirmClick();
        } else if(view.getId() == R.id.dialog_right_negative_btn){
            onBtClickListener.onCancelClick();
        }
    }

    public interface OnBtClickListener {
        void onConfirmClick();
        void onCancelClick();
    }

    public void setOnBtClickListener(OnBtClickListener listener){
        this.onBtClickListener = listener;
    }

    /**
     * 设置title
     * */
    public void setTitle(String title){
        mGenerateTitleTv.setText(title);
    }

    /**
     * 设置对话框的内容view
     * */
    public void setContent(View view){
        mDialogContent.addView(view);
    }

    /**
     * 设置对话框的内容view
     * */
    public void setContent(int resId){
        setContent(inflater.inflate(resId, null));
    }

    /**
     * 显示分割线
     * */
    public void showDivider(boolean show){
        if (show)
            mDialogLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
    }

    public void setLeftBtText(String string){
        mDialogLeftBt.setText(string);
    }
    public void setRightBtText(String string){
        mDialogRightBt.setText(string);
    }

}
